package be.kuleuven.peno3.mobiletoledo.view.course;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.Client.AnnouncementClient;
import be.kuleuven.peno3.mobiletoledo.model.Announcement;
import be.kuleuven.peno3.mobiletoledo.view.messages.Show;

public class AnnouncementView extends Activity {
	private HashMap<String, Integer> announcementIds;
	private LinearLayout ll;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		announcementIds=new HashMap<String, Integer>();
		
		String [] announcements = fetchAnnouncements();
		if (announcements !=null){
			displayAnnouncements(announcements);
		}
			
	} 
	
	@Override
	public void onResume() { // Als men een vraag heeft toegevoegd of beantwoord, en men keert terug naar deze Activity, moet de lijst met vragen geupdated worden.
		super.onResume();
		String [] announcements = fetchAnnouncements();
		if(announcements != null) {
			displayAnnouncements(announcements);
		}
	}
	
	private String[] fetchAnnouncements() {
		int course_id;
		course_id = this.getIntent().getExtras().getInt("course_id"); 
		Announcement[] a;
		a = AnnouncementClient.getAnnouncements(course_id);
		if(a==null) {
			Toast.makeText(this.getApplicationContext(), "Er zijn geen announcements voor dit vak", Toast.LENGTH_SHORT).show();
			finish();
			return null;
		}
		else {
			String[] announcements = new String[a.length];
			for(int i=0;i<a.length;i++) {
				announcements[i] = a[i].getTitle();
				
				announcementIds.put(a[i].getTitle(), a[i].getId());
			}
			return announcements;
		}
	}
	
	/**
	 * Called when the user clicks on a menu-item, and opens the correct Activity
	 * @param view The view that was clicked
	 */
	public void clickListener(View view) {
		TextView v=(TextView) view;
			String title = v.getText().toString();
			int announcementId=announcementIds.get(title);
			Intent i;
			i = new Intent(this, Show.class);
			i.putExtra("announcementId", announcementId);
			startActivity(i);
		
	}
	private void displayAnnouncements(String[] announcements){
		setContentView(R.layout.textlistlayout);
		
		TextView manQuestBar = (TextView) findViewById(R.id.top_bar);
		manQuestBar.setText("Aankondigingen");
		ll = (LinearLayout) findViewById(R.id.man_quest_ll);
		
		for (int i = 0; i < announcements.length; i++){
			if(i > 0){
				ImageView image = new ImageView(this.getApplicationContext());
				image.setImageDrawable(getResources().getDrawable(R.drawable.line));
				ll.addView(image);
			}
			
			TextView text = new TextView(this.getApplicationContext());
			text.setText(announcements[i]);
			text.setClickable(true);
			text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
			text.setGravity(Gravity.CENTER_HORIZONTAL);
			text.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					whenClicked(v);
				}
			}
			);
			ll.addView(text);
		}
	}
	
	/**
	 * Called when the user clicks on a menu-item, and opens the correct Activity
	 * @param view The view that was clicked
	 */
	public void whenClicked(View view) {
		TextView v=(TextView) view;
		String title = v.getText().toString();
		int announcementId=announcementIds.get(title);
		Intent i;
		i = new Intent(this, ShowAnnouncement.class);
		i.putExtra("announcementId", announcementId);
		startActivity(i);
	}
}
